package com.sike.netty.im.server.service;

import cn.hutool.core.util.IdUtil;
import com.sike.netty.im.common.command.CreateGroupRequestCommand;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 群聊服务
 *
 * @author chenssy
 * @date 2023/09/20
 */
public class GroupService {
    /**
     * 群聊
     */
    private static Map<String, List<String>> groupMap = new ConcurrentHashMap<>();

    /**
     * 创建群聊
     *
     * @param requestCommand 创建群聊指令
     * @return 一串
     */
    public String createGroup(CreateGroupRequestCommand requestCommand) {
        // 新建群聊
        String groupId = IdUtil.simpleUUID();
        String groupKey = groupId + "-" + requestCommand.getGroupName();
        groupMap.put(groupKey,requestCommand.getGroupMemberList());

        return groupId;
    }


    /**
     * 加入群聊
     *
     * @param groupId  群聊id
     * @param userName 用户名
     * @return 映射＜字符串，对象＞
     */
    public Map<String, Object> joinGroup(String groupId, String userName) {
        String checkedKey = "";
        List<String> memberList = new ArrayList<>();
        for (Map.Entry<String,List<String>> entry : groupMap.entrySet()) {
            if (entry.getKey().startsWith(groupId)) {
                checkedKey = entry.getKey();
                memberList = entry.getValue();
                break;
            }
        }
        memberList.add(userName);
        groupMap.put(checkedKey,memberList);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("groupName",checkedKey.split("-")[1]);
        resultMap.put("memberList",memberList);
        return resultMap;
    }

    /**
     * 获取群聊组员
     *
     * @param groupId 组id
     * @return 列表＜string＞
     */
    public List<String> listGroupMember(String groupId) {
        return groupMap.entrySet()
                .stream()
                .filter(x -> x.getKey().startsWith(groupId))
                .map(Map.Entry::getValue)
                .findFirst()
                .get();

    }


    /**
     * 退出群聊
     *
     * @param groupId  群聊id
     * @param userName 退出群聊用户名
     * @return groupId- groupName
     */
    public String exitGroup(String groupId, String userName) {
        String groupKey = null;
        for (Map.Entry<String,List<String>> entry : groupMap.entrySet()) {
            if (entry.getKey().startsWith(groupId)) {
                groupKey = entry.getKey();
                entry.getValue().remove(userName);
                break;
            }
        }

        return groupKey;
    }


    /**
     * 获取群聊信息
     *
     * @param groupId 组id
     * @return 映射＜string，列表＜string＞
     */
    public Map<String, Object> getGroupInfo(String groupId) {
        String groupKey = "";
        List<String> memberList = new ArrayList<>();
        for (Map.Entry<String,List<String>> entry : groupMap.entrySet()) {
            if (entry.getKey().startsWith(groupId)) {
                groupKey = entry.getKey();
                memberList = entry.getValue();
                break;
            }
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("groupId",groupKey.split("-")[0]);
        resultMap.put("groupName",groupKey.split("-")[1]);
        resultMap.put("memberList",memberList);
        return resultMap;
    }
}
